package vim.meet.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//@Component
@Aspect
public class TimingAspect {

    @Before("cn.freemethod.pointcut.HaveResultBusinessPointcut.haveResultBusinessPointcut()")
    // @Before("execution(* cn.freemethod.business.HaveResultBusiness.*(..))")
    public void before() {
        System.out.println("TimingAspect before...");
    }

    @Around("cn.freemethod.pointcut.HaveResultBusinessPointcut.haveResultBusinessPointcut()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object retVal = pjp.proceed();
        long end = System.currentTimeMillis();
        System.out.println("time elapse:" + (end - start));
        return retVal;
    }

    @After("cn.freemethod.pointcut.HaveResultBusinessPointcut.haveResultBusinessPointcut()")
    public void after() {
        System.out.println("TimingAspect after...");
    }

    @AfterThrowing(pointcut = "cn.freemethod.pointcut.HaveResultBusinessPointcut.haveResultBusinessPointcut()", throwing = "ex")
    public void doRecoveryActions(Exception ex) {
        System.out.println("@AfterThrowing:" + ex.getMessage());
    }

    @AfterReturning(pointcut = "cn.freemethod.pointcut.HaveResultBusinessPointcut.haveResultBusinessPointcut()", returning = "retVal")
    public void doAccessCheck(Object retVal) {
        System.out.println("@AfterReturning:"+retVal);
    }

}
